package com.rybbaby.tss.core.verifier;

import java.util.Collection;
import java.util.Map;

/**
 * @description 数组、集合元数个数限制验证器
 * 
 * @author 谢进伟
 * 
 * @createDate 2016年3月30日 上午10:33:07
 */
public class SizeVerifier extends AbstractVerifier {
	
	/**
	 * 是否包含左极限
	 * 
	 * @return
	 */
	private boolean includeMin = true;
	
	/**
	 * 是否包含右极限
	 * 
	 * @return
	 */
	private boolean includeMax = true;
	
	/**
	 * 最小极限值(默认包含,若不包含最小极限值,可将 includeMin 设置为false)
	 * 
	 * @return
	 */
	private double min = Double.MIN_VALUE;
	
	/**
	 * 最大极限值(默认包含,若不包含最大极限值,可将 includeMax 设置为false)
	 * 
	 * @return
	 */
	private double max = Double.MAX_VALUE;
	
	/**
	 * 是否允许为空值
	 * 
	 * @return
	 */
	public boolean nullable = true;
	
	/**
	 * 获取 是否允许为空值
	 * 
	 * @return 是否允许为空值
	 */
	public boolean isNullable() {
		return this.nullable;
	}
	
	/**
	 * 设置 是否允许为空值
	 * 
	 * @param nullable
	 *            是否允许为空值
	 */
	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}
	
	/**
	 * 获取是否包含左极限
	 * 
	 * @return 是否包含左极限
	 */
	public boolean isIncludeMin() {
		return this.includeMin;
	}
	
	/**
	 * 设置 是否包含左极限
	 * 
	 * @param includeMin
	 *            是否包含左极限
	 */
	public void setIncludeMin(boolean includeMin) {
		this.includeMin = includeMin;
	}
	
	/**
	 * 获取是否包含右极限
	 * 
	 * @return 是否包含右极限
	 */
	public boolean isIncludeMax() {
		return this.includeMax;
	}
	
	/**
	 * 设置是否包含右极限
	 * 
	 * @param includeMax
	 *            是否包含右极限
	 */
	public void setIncludeMax(boolean includeMax) {
		this.includeMax = includeMax;
	}
	
	/**
	 * 获取最小极限值
	 * 
	 * @return 最小极限值
	 */
	public double getMin() {
		return this.min;
	}
	
	/**
	 * 设置最小极限值(默认包含,若不包含最小极限值,可将 includeMin 设置为false)
	 * 
	 * @param min
	 *            最小极限值
	 */
	public void setMin(double min) {
		this.min = min;
	}
	
	/**
	 * 获取最大极限值
	 * 
	 * @return 最大极限值
	 */
	public double getMax() {
		return this.max;
	}
	
	/**
	 * 设置最大极限值(默认包含,若不包含最大极限值,可将 includeMax 设置为false)
	 * 
	 * @param max
	 *            最大极限值
	 */
	public void setMax(double max) {
		this.max = max;
	}
	
	@Override
	public boolean validate(Object params) {
		return validate(params , nullable , null , new ValidationRule() {
			
			@Override
			public boolean check(Object params) {
				boolean result = true;
				Class<? extends Object> class1 = params.getClass();
				if(class1.isArray()) {
					Object [] array = (Object [])params;
					int value = array.length;
					result = compare(value);
				} else if(params instanceof Collection) {
					Collection<?> list = (Collection<?>)params;
					int value = list.size();
					result = compare(value);
				} else if(params instanceof Map) {
					Map<? , ?> list = (Map<? , ?>)params;
					int value = list.size();
					result = compare(value);
				}
				return result;
			}
		});
	}
	
	private boolean compare(int value) {
		boolean result = true;
		if(!includeMax && !includeMin) {
			result = value > min && value < max;
		} else {
			if(includeMax && includeMin) {
				result = value >= min && value <= max;
			} else if(!includeMax && includeMin) {
				result = value >= min && value < max;
			} else if(includeMax && !includeMin) {
				result = value > min && value <= max;
			}
		}
		return result;
	}
}
